//
//  CTPublishImageView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/15.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPublishImageView.h"
#import "CTHeader.h"
#import "CTShareView.h"
#import "CTAppLanguage.h"
#import "CTDebugManager.h"
#import "UIViewAdditions.h"
#import "CTTextManager.h"
#import "CTTopicView.h"
#import "CTShare.h"
#import <ShareSDK/ShareSDK.h>

@interface CTPublishImageView()<UITextViewDelegate,CTShareViewDelegate,CTTopicViewDelegate>
{
    UIView          *topView;
    UIImageView     *publishImageView;
    UIImageView     *syncImageView;
    UITextView      * publishTextView;
    BOOL            synchronizationBool;
    CTTopicView     *topicView;
    UIImage         * shareImage;
//    NSArray         *topicAry;
    UILabel         *topicLabel;
    UIScrollView *publishScrollView;
}
@end

@implementation CTPublishImageView
@synthesize delegate;

- (void)dealloc
{
    [publishScrollView removeFromSuperview];
    publishScrollView = nil;
    [topicLabel removeFromSuperview];
    topicLabel =nil;
    [topView removeFromSuperview];
    topView = nil;
    [publishTextView removeFromSuperview];
    publishTextView = nil;
    [publishImageView removeFromSuperview];
    publishImageView = nil;
    [topicView removeFromSuperview];
    topicView = nil;
    shareImage = nil;
    
    delegate = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTPublishImageView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
//        synchronizationBool = YES;
        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)];
        [self addGestureRecognizer:tapGesture];
        [self createTitlView];
        [self createPublishImageView];
    }
    return self;
}
//收回键盘
- (void)tapGestureAction:(UITapGestureRecognizer *)sender
{
    [publishTextView resignFirstResponder];
}
- (void)createTitlView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"publishImageView_title" withValue:nil withTable:@"Language"];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.font = [UIFont fontWithName:nil size:19];
    [topView addSubview:titleLabel];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
//    UIButton *sendBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    sendBtn.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-50, 20, 50, 44);
//    [sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [sendBtn setTitleColor:CT_HEADER_WHITE_COLOR forState:UIControlStateNormal];
//    [sendBtn setTitle:[CTAppLanguage localizedStringForKey:@"publishImageView_sendBtn_title" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
//    //    [registerButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//    [topView addSubview:sendBtn];
    
    UILabel * topBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topBottomLine.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
    [topView addSubview:topBottomLine];
}

//创建主View
- (void)createPublishImageView
{
    publishScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topView.bottom, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-topView.height)];
    publishScrollView.contentSize = CGSizeMake(CT_HEADER_SCREEN_WIDTH, 480);
    [self addSubview:publishScrollView];
    
    publishTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, 20, CT_HEADER_SCREEN_WIDTH-105, 75)];
    publishTextView.font = [UIFont systemFontOfSize:15.0f];
    publishTextView.text = [CTAppLanguage localizedStringForKey:@"publishImageView_textView_text" withValue:nil withTable:@"Language"];
    publishTextView.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0f];
    publishTextView.delegate = self;
    publishTextView.textAlignment = NSTextAlignmentLeft;
    publishTextView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [publishScrollView addSubview:publishTextView];
    
    publishImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-85, 15, 75, 75)];
    publishImageView.image = [UIImage imageNamed:@"head"];
    [publishScrollView addSubview:publishImageView];
    
   
    topicLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, publishImageView.bottom+32, CT_HEADER_SCREEN_WIDTH-40, 41)];
//    topicLabel.text = @"话题";
    topicLabel.textAlignment = NSTextAlignmentLeft;
    topicLabel.font = [UIFont systemFontOfSize:12.0f];
    topicLabel.numberOfLines = 2;
    
    topicLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [publishScrollView addSubview:topicLabel];
    
    
    

    
    UILabel * topicBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topicLabel.bottom, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topicBottomLine.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [publishScrollView addSubview: topicBottomLine];
    
    UILabel *shareToSinaLabel = [[UILabel alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-94, topicBottomLine.bottom+12, 84, 12)];
    shareToSinaLabel.font = [UIFont systemFontOfSize:12.0f];
    shareToSinaLabel.text = [CTAppLanguage localizedStringForKey:@"publishImageView_shareToSina_text" withValue:nil withTable:@"Language"];
    shareToSinaLabel.textAlignment = NSTextAlignmentLeft;
    shareToSinaLabel.textColor = CT_HEADER_BLACK_COLOR;
    [publishScrollView addSubview:shareToSinaLabel];
    syncImageView = [[UIImageView alloc]init];
    syncImageView.frame = CGRectMake(shareToSinaLabel.origin.x-21, topicBottomLine.bottom+10, 18,18 );
    if([CTShare isSinaShare] == YES)
    {
        synchronizationBool = YES;
        syncImageView.image = [UIImage imageNamed:@"tongbudao_yitongbu"];

    }
    else
    {
        synchronizationBool = NO;
        syncImageView.image = [UIImage imageNamed:@"tongbu"];

    }
    
//    [shareBtn addTarget:self action:@selector(shareBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [publishScrollView addSubview:syncImageView];
    UIButton *syncBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    syncBtn.frame = CGRectMake(syncImageView.origin.x, syncImageView.origin.y, syncImageView.width+shareToSinaLabel.width, syncImageView.height);
    [syncBtn addTarget:self action:@selector(shareBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [publishScrollView addSubview:syncBtn];
    
    
    CTShareView *shareView = [[CTShareView alloc]init];
    shareView.delegate = self;
    [shareView showShareViewOn:publishScrollView withShareViewType:CTShareViewTypeFree];
    
}
//返回
-(void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewBackBtnAction:)])
    {
        [self.delegate publishImageViewBackBtnAction:self];
    }
}
//同步事件
- (void)shareBtnAction:(UIButton *) sender
{
    if (synchronizationBool==YES)
    {
        syncImageView.image = [UIImage imageNamed:@"tongbu"];
        synchronizationBool = NO;
        [CTShare setSinaShare:synchronizationBool];
    }
    else
    {
        synchronizationBool = YES;
        syncImageView.image = [UIImage imageNamed:@"tongbudao_yitongbu"];
        [CTShare setSinaShare:synchronizationBool];
        [self sinaBtnAction:nil];
    }
    
}
- (void)sinaBtnAction:(CTShareItem *)itm
{
    id<ISSContent> publishContent = [ShareSDK   content:@""
                                       defaultContent:@""
                                                image:nil
                                                title:@""
                                                  url:@""
                                          description:@""
                                            mediaType:SSPublishContentMediaTypeText];
    [ShareSDK shareContent:publishContent
                      type:ShareTypeSinaWeibo
               authOptions:nil
             statusBarTips:YES
                    result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                        
                        if (state == SSPublishContentStateSuccess)
                        {
                            [CTDebugManager showDebugLogInfo:@"发表成功"];
                            
                        }
                        else if (state == SSPublishContentStateFail)
                        {
                            [CTDebugManager showErrorLogInfo:[NSString stringWithFormat:@"发布失败!error code == %ld, error code == %@", (long)[error errorCode], [error errorDescription]]];
                        }
                    }];
    
}

#pragma mark TextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:[CTAppLanguage localizedStringForKey:@"publishImageView_textView_text" withValue:nil withTable:@"Language"]])
    {
        textView.text = nil;
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    
    if([self.delegate respondsToSelector:@selector(publishImageViewPublishTextView:withStr:)])
    {
        [self.delegate publishImageViewPublishTextView:self withStr:textView.text];
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    CTTextManager *textManagre = [CTTextManager shareInstance];
    if ([textManagre isBlankString:textView.text] == YES)
    {
        textView.text = [CTAppLanguage localizedStringForKey:@"publishImageView_textView_text" withValue:nil withTable:@"Language"];
    }
}
#pragma mark -setter-
- (void)setTopicView:(CTFollowList *)followListItem
{
    topicView = [[CTTopicView alloc]initWithFrame:CGRectMake(10, publishImageView.bottom+32, CT_HEADER_SCREEN_WIDTH-20, 15)];
    [topicView creatTopicView:followListItem];
    [publishScrollView addSubview:topicView];
}

- (void)setPublishImage:(UIImage *)publishImage
{
    publishImageView.image = publishImage;
    shareImage = publishImage;
}

- (void)setSynchronizationBool:(BOOL)synBool
{
//    synchronizationBool = synBool;
}
- (void)setTopicArray:(NSArray *)topicArray
{
//    NSString * topicStr= @" ";
//    int str1 = 0;
//    int str2 = 0;
//    int str3 = 0;
//    for (int i = 0; i < 3 ; i++)
//    {
//        NSString * str = [topicArray objectAtIndex:i];
//        if (i == 0)
//        {
//            str1 = (int)str.length+2;
//        }
//        else if(i == 1)
//        {
//            str2 = (int)str.length+2;
//        }
//        else
//        {
//            str3 = (int)str.length+2;
//        }
//        topicStr = [NSString stringWithFormat:@"%@#%@#  ",topicStr,str];
//    }
//    NSLog(@"topicStr = %@",topicStr);
//    topicLabel.text = topicStr;
//    [self setDesRange:NSMakeRange(0,str1) withRange:NSMakeRange(str1,str2) withRange:NSMakeRange(str2,str3)];
    
}
#pragma mark shareViewDelegte

//分享到微信朋友圈
- (void)shareViewShareToWechatFriends:(CTShareView *) view withImage:(UIImage *)image
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewShareToWechatFriends:withImage:)])
    {
        [self.delegate publishImageViewShareToWechatFriends:self withImage:shareImage];
    }
}
//分享到QQ空间
- (void)shareViewShareToQQSpace:(CTShareView *)view withImage:(UIImage *)image
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewShareToQQSpace:withImage:)])
    {
        [self.delegate publishImageViewShareToQQSpace:self withImage:shareImage];
    }
}
//分享到新浪微博
- (void)shareViewShareToSina:(CTShareView *)view withImage:(UIImage *)image
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewShareToSina:withImage:)])
    {
        [self.delegate publishImageViewShareToSina:self withImage:shareImage];
    }
}
//分享到微信
- (void)shareViewShareToWeChat:(CTShareView *)view withImage:(UIImage *)image
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewShareToWeChat:withImage:)])
    {
        [self.delegate publishImageViewShareToWeChat:self withImage:shareImage];
    }
}
//分享到QQ
- (void)shareViewShareToQQ:(CTShareView *)view withImage:(UIImage *)image
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewShareToQQ:withImage:)])
    {
        [self.delegate publishImageViewShareToQQ:self withImage:shareImage];
    }
}

//保存到本地
- (void)shareViewShareToSave:(CTShareView *)view withImage:(UIImage *)image
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewShareToSave:withImage:)])
    {
        [self.delegate publishImageViewShareToSave:self withImage:shareImage];
    }
}

//发布
- (void)shareViewSharePicoPico:(CTShareView *)view
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewSendBtnAction:)])
    {
        [self.delegate publishImageViewSendBtnAction:self];
    }
}
#pragma mark - topicViewDelegate -
- (void)selectTopicAction:(CTTopicView *)view withItem:(CTTopic *)topicitem
{
    if ([self.delegate respondsToSelector:@selector(publishImageViewHitTopicAction:withItem:)])
    {
        [self.delegate publishImageViewHitTopicAction:self withItem:topicitem];
    }
}


- (void)setDesRange:(NSRange)desRange1 withRange:(NSRange)desRang2 withRange:(NSRange)desRange3

{
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:topicLabel.text];
    
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:12.0]
                          range:desRange1];
    
    //    [AttributedStr addAttribute:NSLinkAttributeName value:@"www.baidu.com" range:desRange];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:[UIColor colorWithRed:255/255.0 green:134/255.0 blue:49/255.0 alpha:1.0]
     //    从第N个起 的几个字
                          range:desRange1];
    topicLabel.attributedText = AttributedStr;
    
    NSMutableAttributedString *AttributedStr1 = [[NSMutableAttributedString alloc]initWithString:topicLabel.text];
    
    [AttributedStr1 addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:12.0]
                          range:desRang2];
    
    //    [AttributedStr addAttribute:NSLinkAttributeName value:@"www.baidu.com" range:desRange];
    [AttributedStr1 addAttribute:NSForegroundColorAttributeName
                          value:[UIColor blueColor]
//     [UIColor colorWithRed:255/255.0 green:134/255.0 blue:49/255.0 alpha:1.0]
     //    从第N个起 的几个字
                          range:desRang2];
    topicLabel.attributedText = AttributedStr1;
    
    NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithString:topicLabel.text];
    
    [AttributedStr2 addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:12.0]
                          range:desRange3];
    
    //    [AttributedStr addAttribute:NSLinkAttributeName value:@"www.baidu.com" range:desRange];
    [AttributedStr2 addAttribute:NSForegroundColorAttributeName
                          value:[UIColor redColor]
//     [UIColor colorWithRed:255/255.0 green:134/255.0 blue:49/255.0 alpha:1.0]
     //    从第N个起 的几个字
                          range:desRange3];
    topicLabel.attributedText = AttributedStr2;
    
}
@end
